package grape.attend.rest.group.mvc;

import grape.attend.rest.group.form.GroupAssignUserForm;
import grape.attend.rest.group.form.UserAssignGroupForm;
import grape.attend.rest.group.mapper.GroupUserRelWebMapper;
import grape.attend.rest.group.vo.GroupUserRelVo;
import grape.attend.service.group.api.IGroupUserRelService;
import grape.attend.service.group.po.GroupUserRel;
import grape.common.exception.ExceptionTools;
import grape.common.rest.mvc.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 考勤组与用户关系表，一对多 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
@RestController
@RequestMapping("/attend/groupuserrel")
@Api(tags = "考勤组与用户关系表，一对多相关接口")
public class GroupUserRelController extends BaseController<GroupUserRelVo, GroupUserRel> {

    @Autowired
    private GroupUserRelWebMapper currentWebMapper;
    @Autowired
    private IGroupUserRelService currentService;



    @ApiOperation("考勤组分配用户")
    @PreAuthorize("hasAuthority('groupUserRel:single:groupAssignUser')")
    @PostMapping("/group/assign/user")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean groupAssignUser(@RequestBody @Valid GroupAssignUserForm cf) {
        currentService.groupAssignUser(cf.getGroupId(),cf.getCheckedUserIds(),cf.getUncheckedUserIds(),cf.getIsLazyLoad());
        return true;
    }

    @ApiOperation("根据考勤组ID查询已分配的用户id")
    @PreAuthorize("hasAuthority('groupUserRel:single:queryByGroupId')")
    @GetMapping("/group/{groupId}")
    @ResponseStatus(HttpStatus.OK)
    public List<String> queryByGroupId(@PathVariable String groupId) {
        List<GroupUserRel> rels = currentService.getByGroupId(groupId);
        if (rels == null) {
            throw ExceptionTools.dataNotExistRE("数据不存在");
        }

        return rels.stream().map(item -> item.getUserId()).collect(Collectors.toList());
    }

    @ApiOperation("清空考勤组下的所有用户")
    @PreAuthorize("hasAuthority('groupUserRel:single:deleteByGroupId')")
    @DeleteMapping("/group/{groupId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Boolean deleteByGroupId(@PathVariable String groupId) {
        boolean b = currentService.removeByGroupId(groupId);
        if (!b) {
            throw ExceptionTools.failRE("删除失败，可能要删除的数据不存在，请刷新数据再试");
        }
        return b;
    }


    @ApiOperation("用户分配考勤组")
    @PreAuthorize("hasAuthority('groupUserRel:single:userAssignGroup')")
    @PostMapping("/user/assign/group")
    @ResponseStatus(HttpStatus.CREATED)
    public Boolean userAssignGroup(@RequestBody @Valid UserAssignGroupForm cf) {
        currentService.userAssignGroup(cf.getUserId(),cf.getCheckedGroupIds(),cf.getUncheckedGroupIds(),cf.getIsLazyLoad());
        return true;
    }

    @ApiOperation("根据用户ID查询已分配的用户id")
    @PreAuthorize("hasAuthority('groupUserRel:single:queryByUserId')")
    @GetMapping("/user/{userId}")
    @ResponseStatus(HttpStatus.OK)
    public List<String> queryByUserId(@PathVariable String userId) {
        List<GroupUserRel> rels = currentService.getByUserId(userId);
        if (rels == null) {
            throw ExceptionTools.dataNotExistRE("数据不存在");
        }
        return rels.stream().map(item -> item.getUserId()).collect(Collectors.toList());
    }

    @ApiOperation("清空用户下的所有考勤组")
    @PreAuthorize("hasAuthority('groupUserRel:single:deleteByUserId')")
    @DeleteMapping("/user/{userId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Boolean deleteByUserId(@PathVariable String userId) {
        boolean b = currentService.removeByUserId(userId);
        if (!b) {
            throw ExceptionTools.failRE("删除失败，可能要删除的数据不存在，请刷新数据再试");
        }
        return b;
    }


}
